home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
pctjnv85.arc
/
SURFACE.PAS
< prev
Wrap
Pascal/Delphi Source File
|
1985-08-30
|
6KB
|
228 lines
Program SURFACE;
{- - - - - DEFINE FUNCTION TO BE GRAPHED - - - - - - - - - - - }
Function f(x,y:real):real; { Change this entry in order }
begin { to graph another function. }
f := exp(-(x*y+y*y)/90)*cos((x*x+y*y)/40);
end;
{- - - - - DECLARATIONS- - - - - - - - - - - - - - - - - - - - }
const
xdiv = 40; {* * * These two constants control the }
ydiv = 60; { number of subdivisions of each axis }
xeye = 100; {* * * These three constants determine }
yeye = 10; { the eye position from which the }
zeye = 8; { surface is viewed. }
{ NOTE : xeye and yeye should be nonnegative.}
var
i, j : integer;
xmax, xmin, ymax : real;
ymin, zmax, zmin : real;
xdif, ydif, zdif : real;
p, q : array[0..xdiv,0..ydiv] of integer;
y, z : array[0..xdiv,0..ydiv] of real;
{- - - - - INPUT EXTREME VALUES FOR X, Y - - - - - - - - - - - }
Procedure INPUT_DOMAIN;
begin
write('Enter smallest value of x ');
readln(xmin);
write('Enter largest value of x ');
readln(xmax); xdif := xmax - xmin;
write('Enter smallest value of y ');
readln(ymin);
write('Enter largest value of y ');
readln(ymax); ydif := ymax - ymin;
end;
{- - - - - EVALUATE FUNCTION AT GRID POINTS; - - - - - - - - - }
{- - - - - PROJECT TO VIEW PLANE - - - - - - - - - - - - - - - }
Procedure EVALUATE_AND_PROJECT;
var
xtemp,xtemp1,xtemp2,ytemp,ytemp1,ztemp,xavg,yavg : real;
begin
xavg := (xmax + xmin)/2; yavg := (ymax + ymin)/2;
for i := 0 to xdiv do
for j := 0 to ydiv do
begin
xtemp := xmin + i*xdif/xdiv;
ytemp := ymin + j*ydif/ydiv;
ztemp := f(xtemp,ytemp);
xtemp1 := xeye - xtemp;
ytemp1 := yeye - ytemp;
y[i,j] := (xeye - xavg)*(xeye*ytemp - yeye*xtemp)/
((xeye - xavg)*xtemp1 + (yeye - yavg)*ytemp1);
if y[i,j] <> yeye then
z[i,j] := zeye + (zeye - ztemp)*(y[i,j] - yeye)/ytemp1
else
begin
xtemp2 := yeye*(yavg-yeye)/(xeye-xavg);
z[i,j] := zeye +
(zeye - ztemp)*(xtemp2 - xeye)/xtemp1
end;
end;
end;
{- - - - - DETERMINE PROJECTED EXTREMA - - - - - - - - - - - - }
Procedure FIND_EXTREMA;
var
ytemp,ztemp : real;
begin
ymax := y[0,0]; ymin := ymax;
zmax := z[0,0]; zmin := zmax;
for i := 0 to xdiv do
for j := 0 to ydiv do
begin
ytemp := y[i,j]; ztemp := z[i,j];
if ytemp > ymax then ymax := ytemp;
if ytemp < ymin then ymin := ytemp;
if ztemp > zmax then zmax := ztemp;
if ztemp < zmin then zmin := ztemp;
end;
end;
{- - - - - SCALE TO SCREEN - - - - - - - - - - - - - - - - - - }
Procedure SCALE_TO_SCREEN;
var
dy,dz : real;
begin
dy := (ymax - ymin)/639; dz := (zmax - zmin)/199;
for i := 0 to xdiv do
for j := 0 to ydiv do
begin
p[i,j] := round((y[i,j] - ymin)/dy);
q[i,j] := 199 - round((z[i,j] - zmin)/dz);
end;
end;
{- - - - - EXCHANGE COORDINATES OF TWO POINTS- - - - - - - - - }
Procedure SWAP(var x1,y1,x2,y2:integer);
var
temp : integer;
begin
temp := x1; x1 := x2; x2 := temp;
temp := y1; y1 := y2; y2 := temp;
end;
{- - - - - DRAWS BLANK HORIRONTAL LINE - - - - - - - - - - - - }
Procedure LINE(x0,x1,y:integer);
begin
inline($8B/$BE/x1/ {MOV DI,x1}
$8B/$8E/x0/ {MOV CX,x0}
$39/$CF/ {CMP DI,CX}
$7D/$02/ {JGE 2 bytes}
$87/$F9/ {XCHG CX,DI}
$8B/$96/y/ {MOV DX,y }
$BB/$00/$0C/ {MOV BX,0C00}
$89/$D8/ {L1: MOV AX,BX}
$CD/$10/ {INT 10H}
$41/ {INC CX}
$3B/$F9/ {CMP DI,CX}
$7D/$F7); {JG L1}
end;
{- - - - - BLANKS TRIANGLE - - - - - - - - - - - - - - - - - - }
Procedure TRIBLANK(x0,y0,x1,y1,x2,y2:integer);
var
x3,x4,dx1,dx2,dy1,dy2 : integer;
inc1,inc2,nx1,nx2 : integer;
Procedure BLANK(y:integer);
begin
while y0 < y do
begin
nx1 := nx1 + dx1;
if nx1 > dy1 then
repeat
x3 := x3 + inc1;
nx1 := nx1 - dy1;
until nx1 <= dy1;
nx2 := nx2 + dx2;
if nx2 > dy2 then
repeat
x4 := x4 + inc2;
nx2 := nx2 - dy2;
until nx2 <= dy2;
y0 := y0 + 1;
line(x3,x4,y0);
end;
end;
begin
if y1 < y0 then swap(x0,y0,x1,y1);
if y2 < y0 then swap(x0,y0,x2,y2);
if y2 < y1 then swap(x1,y1,x2,y2);
dy1 := y1 - y0; dy2 := y2 - y0;
if x1 < x0 then inc1 := -1 else inc1 := 1;
if x2 < x0 then inc2 := -1 else inc2 := 1;
dx1 := abs(x1-x0); dx2 := abs(x2-x0);
x3 := x0; x4 := x0;
nx1 := dy1 div 2; nx2 := dy2 div 2;
blank(y1);
if x2 < x1 then inc1 := -1 else inc1 := 1;
x3 := x1; dy1 := y2 - y1;
dx1 := abs(x1 - x2); nx1 := dy1 div 2;
blank(y2);
end;
{- - - - - DRAWS BOX WITH BLANK INTERIOR - - - - - - - - - - - }
Procedure DRAWBOX(x1,y1,x2,y2,x3,y3,x4,y4 : integer);
begin
triblank(x1,y1,x2,y2,x3,y3);
triblank(x2,y2,x3,y3,x4,y4);
draw(x1,y1,x2,y2,1); draw(x1,y1,x3,y3,1);
draw(x2,y2,x4,y4,1); draw(x3,y3,x4,y4,1);
end;
{- - - - - DRAWS SURFACE - - - - - - - - - - - - - - - - - - - }
Procedure GRAPH;
var
x1,x2,x3,x4,y1,y2,y3,y4 : integer;
begin
HiRes; HiResColor(10);
for i := 0 to xdiv-1 do
for j := 0 to ydiv-1 do
begin
x1 := p[i,j]; x2 := p[i+1,j];
x3 := p[i,j+1]; x4 := p[i+1,j+1];
y1 := q[i,j]; y2 := q[i+1,j];
y3 := q[i,j+1]; y4 := q[i+1,j+1];
drawbox(x1,y1,x2,y2,x3,y3,x4,y4);
end;
end;
{- - - - - MAIN PROGRAM BEGINS - - - - - - - - - - - - - - - - }
begin
input_domain;
evaluate_and_project;
find_extrema;
scale_to_screen;
graph;
repeat until keypressed;
TextMode(3);
end.